package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.modules.dic.entity.DicBeGoodAt;
import org.jeecg.common.modules.dic.entity.DicCity;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.jeecg.common.modules.dic.service.IDicCityService;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorConfig;
import org.jeecg.common.modules.doctor.entity.DoctorExtend;
import org.jeecg.common.modules.doctor.mapper.DoctorMapper;
import org.jeecg.common.modules.doctor.service.IDoctorConfigService;
import org.jeecg.common.modules.doctor.service.IDoctorExtendService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.doctor.vo.*;
import org.jeecg.common.util.enums.BusinessTypeEnum;
import org.jeecg.common.util.enums.CertificationStatusEnum;
import org.jeecg.common.util.enums.StatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.print.Doc;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 医生表
 * @Author: jeecg-boot
 * @Date:   2020-08-13
 * @Version: V1.0
 */
@Slf4j
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {

    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private IDicBeGoodAtService dicBeGoodAtService;
    @Resource
    private IDicCityService dicCityService;
    @Resource
    private IDoctorConfigService doctorConfigService;
    @Resource
    private IDoctorExtendService doctorExtendService;

    @Override
    public Doctor getByMobile(String mobile) {
        LambdaQueryWrapper<Doctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Doctor::getMobile,mobile);
        return getOne(queryWrapper);
    }

    @Override
    public Doctor getByName(String name) {
        LambdaQueryWrapper<Doctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Doctor::getName,name);
        return getOne(queryWrapper);
    }

    @Override
    public DoctorWholeVo doctorWhole(String id){
        LambdaQueryWrapper<Doctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Doctor::getId,id);
        Doctor doctor = getOne(queryWrapper);
        if(Objects.isNull(doctor)){
            return null;
        }
        DoctorWholeVo doctorWholeVo = new DoctorWholeVo();
        BeanUtils.copyProperties(doctor,doctorWholeVo);//把patient里在patientVo里的值赋值给patient

        //处理新增的其他元素
        if(doctor.getDepartment() != null){
            DicBeGoodAt dicBeGoodAt = dicBeGoodAtService.getById(doctor.getDepartment());
            doctorWholeVo.setDepartmentName(dicBeGoodAt.getName()); //科室名称
        }
        if(doctor.getProvinceId() != null){
            DicCity province = dicCityService.getById(doctor.getProvinceId());
            doctorWholeVo.setProvinceName(province.getName()); // 省/市名称
        }
        if(doctor.getCityId() != null){
            DicCity city = dicCityService.getById(doctor.getCityId());
            doctorWholeVo.setCityName(city.getName());         // 市/区名称
        }

        //医生配置信息
        DoctorConfig doctorConfig = doctorConfigService.getByDoctorId(doctor.getId(),false);
        doctorWholeVo.setFreeMessageCount(doctorConfig.getFreeMessageCount());
        doctorWholeVo.setTextMoney(doctorConfig.getTextMoney());
        doctorWholeVo.setVideoMoney(doctorConfig.getVideoMoney());
        doctorWholeVo.setRegisterRatio(doctorConfig.getRegisterRatio());
        doctorWholeVo.setWelcomeText(doctorConfig.getWelcomeText());

        //医生擅长信息
        DoctorExtend doctorExtend = doctorExtendService.getByDoctorId(doctor.getId());
        doctorWholeVo.setGood(doctorExtend.getGood());
        doctorWholeVo.setIntroduction(doctorExtend.getIntroduction());
        doctorWholeVo.setAccessToken(doctorExtend.getAccessToken());

        //问诊信息
        doctorWholeVo.setRegisterNums(300);
        doctorWholeVo.setConsultNums(300);
        doctorWholeVo.setFavorableComments(99);

        return doctorWholeVo;
    }

    @Override
    public Doctor registerDoctor(String mobile,String defaultImg) {
        Doctor doctor = new Doctor();
        doctor.setMobile(mobile);
        doctor.setHeadImg(defaultImg);
        doctor.setStatus(StatusEnum.VALID.getCode());
        doctor.setCreateTime(new Date());
        doctor.setBusinessType(BusinessTypeEnum.DOCTOR.getId());
        doctor.setCertificationStatus(CertificationStatusEnum.WAIT_CERTIFICATION.getCode());
        doctor.setShare(10);
        save(doctor);
        return doctor;
    }

    @Override
    public void updateDoctorCertificationStatus(String doctorId, CertificationStatusEnum status,String loginUser) {
        Doctor exists = getById(doctorId);
        exists.setCertificationStatus(status.getCode());
        exists.setUpdateTime(new Date());
        exists.setUpdateBy(loginUser);
        updateById(exists);
    }

    @Override
    public Doctor initAssistant(String name,String mobile,String loginUser){
        Doctor doctor = new Doctor();
        doctor.setName(name);
        doctor.setMobile(mobile);
        doctor.setBusinessType(BusinessTypeEnum.ASSISTANT.getId());
        doctor.setStatus(StatusEnum.VALID.getCode());
        doctor.setCreateBy(loginUser);
        doctor.setCreateTime(new Date());
        doctor.setUpdateBy(loginUser);
        doctor.setUpdateTime(new Date());
        doctor.setCertificationStatus(CertificationStatusEnum.WAIT_CERTIFICATION.getCode());
        save(doctor);
        return doctor;
    }

    @Override
    public List<Doctor> assistantInfo(){
        LambdaQueryWrapper<Doctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Doctor::getBusinessType,1);
        return list(queryWrapper);
    }

    @Override
    public List<DoctorReqVo> getDoctorInfos(String name, Integer certificationStatus, Integer dataStart, Integer pageSize){
        return doctorMapper.selectDoctorInfo(name,certificationStatus,dataStart,pageSize);
    }

    @Override
    public int getDoctorCount(String name, Integer certificationStatus){
        return doctorMapper.getDoctorCount(name,certificationStatus);
    }

    @Override
    public Page<DoctorReqVo> queryDoctors(Page<DoctorReqVo> page, String name, Integer certificationStatus) {
        log.info("--------------------------------into queryDoctors-------------------------------" + page + "~" + name + "~" + certificationStatus);
        List<DoctorReqVo> list = doctorMapper.queryDoctors(page, name, certificationStatus, null, null);
        return page.setRecords(list);
    }
    @Override
    public Page<DoctorReqVo> queryTcmDoctors(Page<DoctorReqVo> page, String name, Integer certificationStatus){
        log.info("--------------------------------into queryDoctors-------------------------------"+page+"~"+name+"~"+certificationStatus);
        List<DoctorReqVo> list = doctorMapper.queryTcmDoctors(page, name, certificationStatus,null,null);
        return page.setRecords(list);
    }

    @Override
    public List<AssistantInfoReqVo> getAssistantInfos(String name,String mobile,String doctorName, Integer dataStart, Integer pageSize){
        return doctorMapper.selectAssistantInfo(name,mobile,doctorName,dataStart,pageSize);
    }

    @Override
    public int getCount(String name,String mobile,String doctorName){
        return doctorMapper.getAssistantCount(name,mobile,doctorName);
    }

    @Override
    public List<Doctor> getDoctorByDepartmentId(int departmentId) {
        LambdaQueryWrapper<Doctor> query = Wrappers.lambdaQuery();
        if(departmentId>0){
            query.eq(Doctor::getDepartment,departmentId);
        }
        query.eq(Doctor::getCertificationStatus,CertificationStatusEnum.CERTIFICATION.getCode());   //已认证
        query.eq(Doctor::getBusinessType,BusinessTypeEnum.DOCTOR.getId());  //医生
        query.eq(Doctor::getStatus,StatusEnum.VALID.getCode());   //有效
        query.eq(Doctor::getIsDisplay,1);   //可展示
        query.last(" order by FIELD(master,1,2,3,4,5,0) asc,sort asc");
//        query.orderByAsc(Doctor::getSort);      //升序排序
        return list(query);
    }

    @Override
    public List<CertInfoReqVo> selectCertInfo(Integer certificationStatus, Integer dataStart, Integer pageSize) {
        return doctorMapper.selectCertInfo(certificationStatus,dataStart,pageSize);
    }

    @Override
    public Integer getCertCount(Integer certificationStatus) {
        return doctorMapper.getCertCount(certificationStatus);
    }

    @Override
    public List<SmDoctorReqVo> getRecommendDoctor(Integer num){
        int pageSize = 10;
        int start = 0;
        if(num != 1){
            start = num  * pageSize;
        }else{
            start = (num - 1) * pageSize;
        }
        return doctorMapper.recommendInfo(start,num);
    }

    @Override
    public List<AssistantInfoReqVo> searchRegisterDoctor(String name){
        return doctorMapper.searchRegisterDoctor(name);
    }
    @Override
    public  List<DoctorReqVo> queryMyDoctors(String openid){
        return doctorMapper.queryMyDoctors(openid);
    };

    @Override
    public  List<DoctorReqVo> getDoctorsByKeyWord(String keyword, String goodDesc){
        return doctorMapper.getDoctorsByKeyWord(keyword, goodDesc);
    };
    @Override
    public  List<DoctorReqVo> getDoctorsByGoodDesc(String goodDesc){
        return doctorMapper.getDoctorsByGoodDesc(goodDesc);
    };
}
